package com.ruoyi.project.module.whd.duidan;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgHands;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/whd/duidan")
public class WhdDuidanController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    /**
     * 查询消费详细列表
     */
   /* @RequiresPermissions("project:whd:duidan:list")*/
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        YqgBarBills yqgBarBills=new YqgBarBills();
        YqgConsumptionDetails ConsumptionDetails=new YqgConsumptionDetails();
        ConsumptionDetails.setEntryName(yqgConsumptionDetails.getEntryName());
        ConsumptionDetails.setMnemonicCode(yqgConsumptionDetails.getMnemonicCode());
        ConsumptionDetails.setProjectCategory(yqgConsumptionDetails.getProjectCategory());
        ConsumptionDetails.setTogether(yqgConsumptionDetails.getTogether());
        yqgBarBills.setHandBrand(yqgConsumptionDetails.getHandBrand());
        yqgBarBills.setHandState("1");
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);

        if(list.size()==0){
            return  getDataTable(new ArrayList<YqgConsumptionDetails>());
        }else {
            if(list.get(0).getRetentionStatus().equals("1")){
                ConsumptionDetails.setBarBillsId(list.get(0).getID());
                ConsumptionDetails.setHandState("1");
                ConsumptionDetails.setRetentionStatus("1");

                List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);

                return  getDataTable(ylist);
            }else {
                ConsumptionDetails.setBeforeBarBillsId(list.get(0).getID());
                ConsumptionDetails.setHandState("1");

                //先查被留出去的记录
                List<YqgConsumptionDetails> llist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);

                ConsumptionDetails.setBeforeBarBillsId(null);
                ConsumptionDetails.setBarBillsId(list.get(0).getID());
                //再查自己的记录
                List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
                //过滤留进来的记录
                List<YqgConsumptionDetails> removelist=new ArrayList<>();
                for(int i=0;i<ylist.size();i++){
                    if(ylist.get(i).getBeforeBarBillsId()==null){

                    }else{
                        if (!ylist.get(i).getBeforeBarBillsId().equals(list.get(0).getID())){
                            removelist.add(ylist.get(i));
                        }
                    }

                }
                ylist.removeAll(removelist);
                if(llist.size()!=0){
                    ylist.addAll(llist);
                }

                return  getDataTable(ylist);
            }
        }
    }

    /**
     * 导出消费详细列表
     */
    /*@RequiresPermissions("project:whd:duidan:export")*/
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
   /* @RequiresPermissions("project:whd:duidan:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        YqgHands hands=new YqgHands();
        hands.setHandBrand(id);
        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandBrand(id);
        yqgBarBills.setHandState("1");
        /*hands.setHandState();*/
        List<YqgHands> wulist = yqgHandsService.selectYqgHandsList(hands);
        if(wulist.size()==0){
            return  new AjaxResult(500,id+"手牌不存在，请输入正确的手牌号");
        }else{
            List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
            if(list.size()==0){
                return  new AjaxResult(500,id+"手牌不在台，请输入在台手牌");
            }else{
                return  new AjaxResult(200,"");
            }
        }

    }

    /**
     * 新增消费详细
     */
    /*@RequiresPermissions("project:whd:duidan:add")*/
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
   /* @RequiresPermissions("project:whd:duidan:edit")*/
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
   /* @RequiresPermissions("project:whd:duidan:remove")*/
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
    /**
     * 未下钟 未退房不能结账  提示
     */
   /* public AjaxResult feedback(String handbrand,String mergeStatus,String retentionStatus )
    {
        int flag=check(handbrand,"0","1");
        System.out.println(flag);
        if(flag==2){
            System.out.println(handbrand);
            System.out.println("未退房");
        }
        else if (flag==3){
            System.out.println(handbrand);
            System.out.println("未下钟房");
        }
    }*/
    /**
     * 未下钟 未退房不能结账  查库
     */
    public int check(String handbrand,String mergeStatus,String retentionStatus ){
        // handbrand 手牌号 mergeStatus 并牌状态 RetentionStatus 留牌状态
        //查询 根据手牌号 和 并牌 留牌状态 唯一确定一个
        //2未退房  3未下钟 1全退
        YqgConsumptionDetails yqgConsumptionDetails=new YqgConsumptionDetails();

        if(retentionStatus=="0"){
            //留牌查留前手牌
            yqgConsumptionDetails.setBeforeLeave(handbrand);
        }else{
            //并牌查手牌号
            yqgConsumptionDetails.setHandBrand(handbrand);
        }

        yqgConsumptionDetails.setHandState("1");
        yqgConsumptionDetails.setMergeStatus(mergeStatus);
        yqgConsumptionDetails.setRetentionStatus(retentionStatus);
        //设置未下钟
        yqgConsumptionDetails.setNextClockStatus("1");
        //自己的账单
        List<YqgConsumptionDetails> clocklist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);

        if(clocklist.size()==0){
            yqgConsumptionDetails.setNextClockStatus("");
            //设置未退房
            yqgConsumptionDetails.setCheckOutStatus("1");
            List<YqgConsumptionDetails> checklist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            if (checklist.size()==0){
                return  1;
            }
            else {
                //未退房
                return  2;
            }

        }
        else{
            //未下钟
            return  3;
        }

    }
}
